package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.DailyQueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper mapper;

    @Autowired
    private DailyContentMapper dailyContentMapper;

    public PageInfo<Daily> selectForList(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Daily> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(Daily daily) {
        //获取日报对象
        DailyContent content = daily.getDailyContent();
        Date date = new Date();
        daily.setSchedule(date);
        if (daily.getId() == null) {
            mapper.insert(daily);
            //根据新增返回的ID设值日报ID
            content.setId(daily.getId());
            dailyContentMapper.insert(content);

        } else {
            content.setId(daily.getId());
            dailyContentMapper.updateByPrimaryKey(content);
            mapper.updateByPrimaryKey(daily);
        }

    }

    @Override
    public Daily selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DailyContent getContentById(Long id) {
        return dailyContentMapper.selectContentById(id);

    }

    @Override
    public PageInfo<Daily> selectBySchedule(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Daily> list = mapper.selectBySchedule(qo);
        return new PageInfo<>(list);

    }

    @Override
    public PageInfo<Daily> getDaily(DailyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Daily> list = mapper.selectDaily(qo);
        return new PageInfo<>(list);
    }



}
